﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace ThatsMeSDK.Core
{
    public class ImageManipulator
    {

        /// <summary>
        /// Creates a frame around an image
        /// </summary>
        /// <param name="image">Original image to frame</param>
        /// <param name="width">Size of the frame</param>
        /// <param name="color">Color of the frame</param>
        /// <returns>Framed image</returns>
        public Image FrameImage(Image image, int width, Color color)
        {
            Pen p = new Pen(new SolidBrush(color), width);
            Graphics g = Graphics.FromImage(image);

            g.DrawRectangle(p, new Rectangle
                (0, 0, image.Size.Width - (width / 2), image.Size.Height - (width / 2)));
            g.Dispose();

            return image;
        }

        /// <summary>
        /// Cuts a rectangle out of an image
        /// </summary>
        /// <param name="image">Original image to crop</param>
        /// <param name="cropArea">Aria to crop</param>
        /// <returns>Cropped image</returns>
        public Image CropImage(Image image, Rectangle cropArea)
        {
            Bitmap bmpCrop;
            Bitmap bmpImage = new Bitmap(image);

            if (image.Width < cropArea.Width)
            {
                cropArea.Width = image.Width;
            }

            if (image.Height < cropArea.Height)
            {
                cropArea.Height = image.Height;
            }

            bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        /// <summary>
        /// Resizes an image proportional
        /// </summary>
        /// <param name="image">Original image to resize</param>
        /// <param name="size">Size of the new image (fit range)</param>
        /// <param name="fillArea">If true, image fills the whole area</param>
        /// <returns>Rezised image</returns>
        public Image ResizeImage(Image image, Size size, bool fillArea)
        {
            int sourceWidth = image.Width;
            int sourceHeight = image.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (fillArea)
            {
                if (nPercentH > nPercentW)
                {
                    nPercent = nPercentH;
                }
                else
                {
                    nPercent = nPercentW;
                }
            }
            else
            {
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                }
                else
                {
                    nPercent = nPercentW;
                }
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(image, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }
    }
}
